// pages/menu/commodity-price/price-detail/edit-price/edit-price.js
var app = getApp()
var verify = require('../../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var commodityPriceDetail = JSON.parse(options.commodityPriceDetail);
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      commodityPriceDetail: commodityPriceDetail[options.ind],
			ind:options.ind,
			commodityPriceDetails:commodityPriceDetail,
      productId: options.productId,
      commodityPriceName: options.commodityPriceName,
      commodityPriceId: options.commodityPriceId
    })
  },

  // 修改商品显示名
  editShowName(res) {
    var showName = res.detail.value
    var commodityPriceDetail = this.data.commodityPriceDetail
    commodityPriceDetail.commodityShowName = showName
    this.setData({
      commodityPriceDetail: commodityPriceDetail
    })
  },

  // 修改商品价格
  editBPrice(res){
    var price = res.detail.value
    if (!/^(\d?)+(\.\d{0,2})?$/.test(price)) { //正则验证，金额小数点后不能大于两位数字
      price = price.substring(0, price.length - 1);
    }
    var commodityPriceDetail = this.data.commodityPriceDetail
    commodityPriceDetail.userPrices.priceCommodity = price
    this.setData({
      commodityPriceDetail: commodityPriceDetail
    })
  },

  // 修改用户商品参数价格
  touchUPrice(res){
    var index = res.currentTarget.dataset.index;
    var idx = res.currentTarget.dataset.idx;
    this.setData({
      index:index,
      idx:idx
    })
  },
  editUPrice(res){
    var index = this.data.index
    var idx = this.data.idx
    var price = res.detail.value
    if (!/^(\d?)+(\.\d{0,2})?$/.test(price)) { //正则验证，金额小数点后不能大于两位数字
      price = price.substring(0, price.length - 1);
    }
    var commodityPriceDetail = this.data.commodityPriceDetail
    commodityPriceDetail.userPrices.priceProperty.userGoodsDef[index].pvalus[idx] = price
    this.setData({
      commodityPriceDetail: commodityPriceDetail
    })
  },

  // 保存全部参数
  save(){
    var commodityPriceDetail = this.data.commodityPriceDetails;
    commodityPriceDetail[this.data.ind] = this.data.commodityPriceDetail;
    var productId = this.data.productId;
    var commodityPriceName = this.data.commodityPriceName;
    var commodityPriceId = this.data.commodityPriceId;
    var delete1 = [];
    // console.log("commodityPriceId = ", commodityPriceId)
   // console.log("commodityPriceDetail = ", commodityPriceDetail)
    // console.log("commodityPriceName = ", commodityPriceName)
    // console.log("productId = ", productId)

    var token = app.globalData.token;
    var baseUrl = app.globalData.baseUrl;
    var that = this;
    wx.request({
      url: baseUrl + 'commodity_price/' + commodityPriceId,
      method:"put",
      header: {
        'AUTHORIZATION': token
      },
      data:{
        commodityPriceDetail: commodityPriceDetail,
        commodityPriceName: commodityPriceName,
        productId:productId,
        delete: delete1
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功!',
            duration: 3500
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  }
})